const express = require('express');
const router = express.Router();

// 模拟延迟函数
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));

// 生成模拟POI数据
const generatePoiData = (location, keywords = '', radius = 3000, types = '') => {
  const basePois = [
    {
      id: 'B0FFJQ1QJN',
      name: '广州塔',
      type: '风景名胜',
      typecode: '110000',
      address: '广东省广州市海珠区阅江西路222号',
      location: '113.289405,23.013125',
      distance: '500',
      tel: '020-89338222',
      website: 'www.cantontower.com',
      email: 'info@cantontower.com',
      pcode: '440000',
      citycode: '020',
      adcode: '440105',
      cityname: '广州市',
      adname: '海珠区',
      importance: '1',
      shopinfo: '0',
      poiweight: '1',
      gridcode: '440105001001',
      navi_poiid: 'B0FFJQ1QJN',
      entrances: [
        {
          location: '113.289405,23.013125',
          name: '正门'
        }
      ],
      exits: [
        {
          location: '113.289405,23.013125',
          name: '正门'
        }
      ]
    },
    {
      id: 'B0FFJQ1QJO',
      name: '珠江新城地铁站',
      type: '交通设施',
      typecode: '150000',
      address: '广东省广州市天河区珠江新城',
      location: '113.287405,23.011125',
      distance: '800',
      tel: '020-96900',
      pcode: '440000',
      citycode: '020',
      adcode: '440106',
      cityname: '广州市',
      adname: '天河区',
      importance: '1',
      shopinfo: '0',
      poiweight: '1',
      gridcode: '440106001001',
      navi_poiid: 'B0FFJQ1QJO'
    },
    {
      id: 'B0FFJQ1QJP',
      name: '广州图书馆',
      type: '科教文化',
      typecode: '140000',
      address: '广东省广州市天河区珠江新城花城大道',
      location: '113.286405,23.012125',
      distance: '1200',
      tel: '020-83836666',
      website: 'www.gzlib.gov.cn',
      pcode: '440000',
      citycode: '020',
      adcode: '440106',
      cityname: '广州市',
      adname: '天河区',
      importance: '1',
      shopinfo: '0',
      poiweight: '1',
      gridcode: '440106001002',
      navi_poiid: 'B0FFJQ1QJP'
    },
    {
      id: 'B0FFJQ1QJQ',
      name: '星巴克咖啡(珠江新城店)',
      type: '餐饮服务',
      typecode: '050000',
      address: '广东省广州市天河区珠江新城花城大道85号',
      location: '113.285405,23.013125',
      distance: '1500',
      tel: '020-12345678',
      pcode: '440000',
      citycode: '020',
      adcode: '440106',
      cityname: '广州市',
      adname: '天河区',
      importance: '1',
      shopinfo: '1',
      poiweight: '1',
      gridcode: '440106001003',
      navi_poiid: 'B0FFJQ1QJQ'
    },
    {
      id: 'B0FFJQ1QJR',
      name: '广州银行(珠江新城支行)',
      type: '金融保险',
      typecode: '160000',
      address: '广东省广州市天河区珠江新城花城大道100号',
      location: '113.284405,23.014125',
      distance: '1800',
      tel: '020-87654321',
      pcode: '440000',
      citycode: '020',
      adcode: '440106',
      cityname: '广州市',
      adname: '天河区',
      importance: '1',
      shopinfo: '0',
      poiweight: '1',
      gridcode: '440106001004',
      navi_poiid: 'B0FFJQ1QJR'
    },
    {
      id: 'B0FFJQ1QJS',
      name: '万达广场(珠江新城店)',
      type: '购物服务',
      typecode: '060000',
      address: '广东省广州市天河区珠江新城花城大道200号',
      location: '113.283405,23.015125',
      distance: '2200',
      tel: '020-98765432',
      pcode: '440000',
      citycode: '020',
      adcode: '440106',
      cityname: '广州市',
      adname: '天河区',
      importance: '1',
      shopinfo: '1',
      poiweight: '1',
      gridcode: '440106001005',
      navi_poiid: 'B0FFJQ1QJS'
    },
    {
      id: 'B0FFJQ1QJT',
      name: '广州医科大学附属第一医院',
      type: '医疗保健',
      typecode: '090000',
      address: '广东省广州市天河区珠江新城花城大道300号',
      location: '113.282405,23.016125',
      distance: '2500',
      tel: '020-12345678',
      pcode: '440000',
      citycode: '020',
      adcode: '440106',
      cityname: '广州市',
      adname: '天河区',
      importance: '1',
      shopinfo: '0',
      poiweight: '1',
      gridcode: '440106001006',
      navi_poiid: 'B0FFJQ1QJT'
    }
  ];

  let filteredPois = [...basePois];

  // 根据关键词过滤
  if (keywords) {
    const keyword = keywords.toLowerCase();
    filteredPois = filteredPois.filter(poi => 
      poi.name.toLowerCase().includes(keyword) || 
      poi.type.toLowerCase().includes(keyword)
    );
  }

  // 根据类型过滤
  if (types) {
    const type = types.toLowerCase();
    filteredPois = filteredPois.filter(poi => 
      poi.type.toLowerCase().includes(type)
    );
  }

  // 根据半径过滤
  const radiusNum = parseInt(radius);
  filteredPois = filteredPois.filter(poi => 
    parseInt(poi.distance) <= radiusNum
  );

  return {
    success: true,
    data: filteredPois,
    count: filteredPois.length,
    message: 'POI搜索成功'
  };
};

// GET /api/poi - POI搜索
router.get('/', async (req, res) => {
  try {
    const { location, keywords, radius = 3000, types } = req.query;
    
    // 参数验证
    if (!location) {
      return res.status(400).json({
        success: false,
        message: '缺少location参数'
      });
    }
    
    // 模拟网络延迟
    await delay(400);
    
    // 生成模拟数据
    const poiData = generatePoiData(location, keywords, radius, types);
    
    res.json(poiData);
  } catch (error) {
    console.error('POI搜索API错误:', error);
    res.status(500).json({
      success: false,
      message: 'POI搜索失败',
      error: error.message
    });
  }
});

// GET /api/poi/nearby - 附近POI搜索
router.get('/nearby', async (req, res) => {
  try {
    const { location, radius = 1000 } = req.query;
    
    if (!location) {
      return res.status(400).json({
        success: false,
        message: '缺少location参数'
      });
    }
    
    await delay(300);
    
    const nearbyData = {
      success: true,
      data: [
        {
          id: 'B0FFJQ1QJN',
          name: '广州塔',
          type: '风景名胜',
          distance: '500',
          location: '113.289405,23.013125'
        },
        {
          id: 'B0FFJQ1QJO',
          name: '珠江新城地铁站',
          type: '交通设施',
          distance: '800',
          location: '113.287405,23.011125'
        }
      ],
      count: 2,
      message: '附近POI搜索成功'
    };
    
    res.json(nearbyData);
  } catch (error) {
    console.error('附近POI搜索API错误:', error);
    res.status(500).json({
      success: false,
      message: '附近POI搜索失败',
      error: error.message
    });
  }
});

// GET /api/poi/detail - POI详情
router.get('/detail/:id', async (req, res) => {
  try {
    const { id } = req.params;
    
    await delay(200);
    
    const detailData = {
      success: true,
      data: {
        id: id,
        name: '广州塔',
        type: '风景名胜',
        address: '广东省广州市海珠区阅江西路222号',
        location: '113.289405,23.013125',
        tel: '020-89338222',
        website: 'www.cantontower.com',
        email: 'info@cantontower.com',
        description: '广州塔是广州市的地标性建筑，塔高600米，是世界第三高塔。',
        opening_hours: '09:00-22:00',
        ticket_price: '150元',
        photos: [
          'https://example.com/photo1.jpg',
          'https://example.com/photo2.jpg'
        ],
        rating: 4.5,
        reviews: 1234
      },
      message: 'POI详情获取成功'
    };
    
    res.json(detailData);
  } catch (error) {
    console.error('POI详情API错误:', error);
    res.status(500).json({
      success: false,
      message: 'POI详情获取失败',
      error: error.message
    });
  }
});

module.exports = router; 